#ifndef _SG_PACKET_PARSER_H_
#define _SG_PACKET_PARSER_H_

#include "IPacketParser.h"
#include <google/protobuf/message_lite.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>

// |4 bytes(msglen)|N bytes(msg body)
class CSGPacketParser : public SGLib::IPacketParser
{
public:
    s32 GetHeadLen()
    { 
        return sizeof(s32); 
    }
    s32 GetPacketLen(const byte *data)
    {
        google::protobuf::uint32 msz = 0;
        /*
        const uint8* ptr = data;
        uint32 b;
        uint32 result = 0;
        b = *(ptr++); result  = (b & 0x7F)      ; if (!(b & 0x80)) goto done;
        b = *(ptr++); result |= (b & 0x7F) <<  7; if (!(b & 0x80)) goto done;
        b = *(ptr++); result |= (b & 0x7F) << 14; if (!(b & 0x80)) goto done;
        b = *(ptr++); result |= (b & 0x7F) << 21; if (!(b & 0x80)) goto done;
        b = *(ptr++); result |=  b         << 28; if (!(b & 0x80)) goto done;
        for (int i = 0; i < kMaxVarintBytes - kMaxVarint32Bytes; i++) {
            b = *(ptr++); if (!(b & 0x80)) goto done;
        }
        return -1;
done:
        return result;
        */
        google::protobuf::io::ArrayInputStream arrayInputStream(data, 10);
        google::protobuf::io::CodedInputStream codedInputStream(&arrayInputStream);
        codedInputStream.ReadVarint32(&msz);

        return (s32)msz + GetHeadLen();
        //*/
    }
    void FillHeader(byte *data, s32 len, u32 type)
    {
        *((s32*)data) = len;
    }
    virtual u32 GetMsgId(const byte *data)
    {
        return 0;
    }
};

#endif

